//
//  ProjectDataBaseViewController.m
//  YellowBridge
//
//  Created by 王右 on 2018/12/3.
//  Copyright © 2018 王右. All rights reserved.
//

#import "ProjectDataBaseViewController.h"
#import "ProjectDataBaseCell.h"
#import "AddNewProjectController.h"
#import "ProjectDeatailViewController.h"
#import "DropDownListView.h"
#import "ProjectListModel.h"

@interface ProjectDataBaseViewController ()<UISearchBarDelegate,UITableViewDelegate,UITableViewDataSource>

@property (strong, nonatomic) IBOutlet UIView *headView;

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;

@property (weak, nonatomic) IBOutlet QMUIButton *projectCategoryButton;

@property (nonatomic, strong) DropDownListView *projectCategoryDropDownView;

@property (weak, nonatomic) IBOutlet QMUIButton *projectBelongButton;

@property (nonatomic, strong) DropDownListView *projectBelongDropDownView;

@property (weak, nonatomic) IBOutlet QMUIButton *projectSizeButton;

@property (nonatomic, strong) DropDownListView *projectSizeDropDownView;

@property (weak, nonatomic) IBOutlet UITableView *baseTableView;

@property (weak, nonatomic) IBOutlet UIButton *addNewProjectButton;

@property (nonatomic, strong) UIView *containerTostView;

@property (nonatomic, strong) NSArray *dataArr;

@property (nonatomic, strong) NSString *categorySortString;

@property (nonatomic, strong) NSString *belongSortSting;

@property (nonatomic, strong) NSString *sizeSortString;

@property (nonatomic, strong) NSMutableArray *showDataArr;

@property (nonatomic, strong) NSMutableArray *searchResultArr;

@property (nonatomic, strong) NSMutableDictionary *parmDic;
@end

@implementation ProjectDataBaseViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"项目资源库";
    
    _parmDic = @{@"account":User_Account,@"token":User_SignToken,@"projectname":@"",@"pmtype":@"",@"pmxz":@"",@"pmgm":@""}.mutableCopy;
    
    self.categorySortString = @"0";
    self.belongSortSting = @"0";
    self.sizeSortString = @"0";
    
    [self configHeadView];
    _baseTableView.tableHeaderView = _headView;
}


- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getProjectListData];
}


- (void)configHeadView{
    _projectCategoryButton.imagePosition = QMUIButtonImagePositionRight;
    _projectSizeButton.imagePosition = QMUIButtonImagePositionRight;
    _projectBelongButton.imagePosition = QMUIButtonImagePositionRight;
    
    for(UIView *subView in [_searchBar subviews]) {
        if([subView conformsToProtocol:@protocol(UITextInputTraits)]) {
            [(UITextField *)subView setReturnKeyType: UIReturnKeyDone];
        } else {
            for(UIView *subSubView in [subView subviews]) {
                if([subSubView conformsToProtocol:@protocol(UITextInputTraits)]) {
                    [(UITextField *)subSubView setReturnKeyType: UIReturnKeyDone];
                }
            }
        }
    }
}

- (void)getProjectListData{
    [self hudWithText:@"加载中..."];
    [RequestManager postRequestWithModule:@"v1/projectlist" param:_parmDic model:nil success:^(NSDictionary *obj) {
        [self removeHud];
        NSDictionary *dic = obj[@"result"];
        self->_dataArr = [ProjectListModel objectWithArray:dic[@"plist"]];
        self->_showDataArr = self->_dataArr.mutableCopy;
        [self->_baseTableView reloadData];
    } failure:^(NSString *error) {
        [self removeHud];
        [self.view makeToast:error];
    }];
}



- (IBAction)sortByCategoryAction:(QMUIButton *)sender {
    sender.selected = !sender.selected;
    _projectBelongButton.selected = NO;
    _projectSizeButton.selected = NO;
    [_searchBar resignFirstResponder];
    
    if (!_projectCategoryDropDownView) {
        [self creatDropDownListViewWithButton:sender];
    }else{
        _projectCategoryDropDownView.hidden = !_projectCategoryDropDownView.hidden;
        _projectBelongDropDownView.hidden = YES;
        _projectSizeDropDownView.hidden = YES;
        if (_projectCategoryDropDownView.hidden) {
            _containerTostView.hidden = YES;
        }else{
            _containerTostView.hidden = NO;
        }
    }
}

- (IBAction)sortByFeatureAction:(QMUIButton *)sender {
    sender.selected = !sender.selected;
    _projectCategoryButton.selected = NO;
    _projectSizeButton.selected = NO;
    [_searchBar resignFirstResponder];
    if (!_projectBelongDropDownView) {
        [self creatDropDownListViewWithButton:sender];
    }else{
        
        _projectBelongDropDownView.hidden = !_projectBelongDropDownView.hidden;
        _projectCategoryDropDownView.hidden = YES;
        _projectSizeDropDownView.hidden = YES;
        
        if (_projectBelongDropDownView.hidden) {
            _containerTostView.hidden = YES;
        }else{
            _containerTostView.hidden = NO;
        }
    }
}

- (IBAction)sortBySizeAction:(QMUIButton *)sender {
    sender.selected = !sender.selected;
    _projectCategoryButton.selected = NO;
    _projectBelongButton.selected = NO;
    [_searchBar resignFirstResponder];
    if (!_projectSizeDropDownView) {
        [self creatDropDownListViewWithButton:sender];
    }else{
        _projectSizeDropDownView.hidden = !_projectSizeDropDownView.hidden;
        _projectBelongDropDownView.hidden = YES;
        _projectCategoryDropDownView.hidden = YES;
        
        if (_projectSizeDropDownView.hidden) {
            _containerTostView.hidden = YES;
        }else{
            _containerTostView.hidden = NO;
        }
    }
}

- (void)sortProject{
    
//    __block NSMutableArray *mutableArr = [NSMutableArray arrayWithArray:_searchBar.text.length==0?_dataArr:_searchResultArr];
//
//    BOOL isInitCategorySortString = [_categorySortString isEqualToString:@"0"];
//    BOOL isInitBelongSortString = [_belongSortSting isEqualToString:@"0"];
//    BOOL isInitSizeSrotString = [_sizeSortString isEqualToString:@"0"];
//
//
//    [_dataArr enumerateObjectsUsingBlock:^(ProjectListModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//
//        //移除不符合项目类别条件的项目
//        if (!isInitCategorySortString) {
//            if (![obj.pmtype isEqualToString:self->_categorySortString]) {
//                [mutableArr removeObject:obj];
//            }
//        }
//        //移除不符合项目性质条件的项目
//        if (!isInitBelongSortString) {
//            if (![obj.pmxz isEqualToString:self->_belongSortSting]) {
//                [mutableArr removeObject:obj];
//            }
//        }
//        //移除不符合项目规模的项目
//        if (!isInitSizeSrotString) {
//            if (![obj.pmgm isEqualToString:self->_sizeSortString]) {
//                [mutableArr removeObject:obj];
//            }
//        }
//    }];
//
//    _showDataArr = mutableArr;
//    [_baseTableView reloadData];
    
    [self getProjectListData];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _showDataArr.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 210;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ProjectDataBaseCell *cell = (ProjectDataBaseCell *)[tableView cellWithCellNibName:@"ProjectDataBaseCell"];
    cell.model = _dataArr[indexPath.row];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [_searchBar resignFirstResponder];
    ProjectListModel *model = _dataArr[indexPath.row];
    ProjectDeatailViewController *vc = [ProjectDeatailViewController new];
    vc.projectID = model.projectID;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_searchBar resignFirstResponder];
}

//取消按钮执行方法
-(void)searchBarCancelButtonClicked:(UISearchBar *)searchBar{
    
}
//搜索按钮执行方法
- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar{
    [searchBar resignFirstResponder];
    if (searchBar.text.length == 0) {
//        __block NSMutableArray *searchArr = [NSMutableArray arrayWithArray:_dataArr];
//        [_dataArr enumerateObjectsUsingBlock:^(ProjectListModel *_Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//            if (![obj.projectname containsString:searchBar.text]) {
//                [searchArr removeObject:obj];
//            }
//        }];
//        _showDataArr = searchArr;
//        _searchResultArr = searchArr;
//        [_baseTableView reloadData];
        [_parmDic setValue:@"" forKey:@"projectname"];

    }else{
        [_parmDic setValue:searchBar.text forKey:@"projectname"];
    }
    [self sortProject];
}


- (IBAction)addNewProjectButtonAction:(UIButton *)sender {
    //重置状态
    _projectCategoryButton.selected = NO;
    _projectBelongButton.selected = NO;
    _projectSizeButton.selected = NO;
    _containerTostView.hidden = YES;
    [self.navigationController pushViewController:[AddNewProjectController new] animated:YES];
}

- (void)creatDropDownListViewWithButton:(QMUIButton *)sender{
    NSArray *dataArr;
    __weak __typeof(self)weakSelf = self;
    if (sender == _projectCategoryButton) {
        dataArr = @[@"全部类型",@"市政道路",@"居住建筑",@"公共配套",@"其他"];
        DropDownListView * alert =  [[DropDownListView alloc] initWithFrame:CGRectMake(sender.frame.origin.x, 0, sender.bounds.size.width, 40 * dataArr.count)];
        self.projectCategoryDropDownView = alert;
        alert.arrMDataSource = dataArr.mutableCopy;
        [alert setDidSelectedCallback:^(NSInteger index, NSString *content) {
            //回调中要实现的功能.
            [sender setTitle:content forState:(UIControlStateNormal)];
//            weakSelf.categorySortString = [NSString stringWithFormat:@"%@",@(index)];
            if (index==0) {
                [weakSelf.parmDic setValue:@"" forKey:@"pmtype"];
                
            }else{
                [weakSelf.parmDic setValue:[NSString stringWithFormat:@"%@",@(index)] forKey:@"pmtype"];
            }
            [weakSelf sortByCategoryAction:sender];
            [weakSelf sortProject];
        }];
        [self.containerTostView addSubview:_projectCategoryDropDownView];
        self.containerTostView.hidden = NO;
        _projectBelongDropDownView.hidden = YES;
        _projectSizeDropDownView.hidden = YES;
    }else if (sender == _projectBelongButton){
        dataArr = @[@"全部性质",@"新建项目",@"扩建项目",@"改建项目"];
        DropDownListView * alert =  [[DropDownListView alloc] initWithFrame:CGRectMake(sender.frame.origin.x, 0, sender.bounds.size.width, 40 * dataArr.count)];
        self.projectBelongDropDownView = alert;
        alert.arrMDataSource = dataArr.mutableCopy;
        [alert setDidSelectedCallback:^(NSInteger index, NSString *content) {
            //回调中要实现的功能.
            [sender setTitle:content forState:(UIControlStateNormal)];
//            weakSelf.belongSortSting = [NSString stringWithFormat:@"%@",@(index)];
            if (index==0) {
                [weakSelf.parmDic setValue:@"" forKey:@"pmxz"];

            }else{
                [weakSelf.parmDic setValue:[NSString stringWithFormat:@"%@",@(index)] forKey:@"pmxz"];
            }

            [weakSelf sortByFeatureAction:sender];
            [weakSelf sortProject];
        }];
        [self.containerTostView addSubview:_projectBelongDropDownView];
        self.containerTostView.hidden = NO;
        _projectCategoryDropDownView.hidden = YES;
        _projectSizeDropDownView.hidden = YES;
    }else{
        dataArr = @[@"全部规模",@"大型",@"中型",@"小型"];
        DropDownListView * alert =  [[DropDownListView alloc] initWithFrame:CGRectMake(sender.frame.origin.x, 0, sender.bounds.size.width, 40 * dataArr.count)];
        self.projectSizeDropDownView = alert;
        alert.arrMDataSource = dataArr.mutableCopy;
        [alert setDidSelectedCallback:^(NSInteger index, NSString *content) {
            //回调中要实现的功能.
            [sender setTitle:content forState:(UIControlStateNormal)];
            switch (index) {
                case 0:
                    weakSelf.sizeSortString = @"0";
                    [weakSelf.parmDic setValue:@"" forKey:@"pmgm"];

                    break;
                case 1:
//                    weakSelf.sizeSortString = @"A";
                    [weakSelf.parmDic setValue:@"A" forKey:@"pmgm"];

                    break;
                case 2:
//                    weakSelf.sizeSortString = @"B";
                    [weakSelf.parmDic setValue:@"B" forKey:@"pmgm"];

                    break;
                default:
//                    weakSelf.sizeSortString = @"C";
                    [weakSelf.parmDic setValue:@"C" forKey:@"pmgm"];

                    break;
            }
            [weakSelf sortBySizeAction:sender];
            [weakSelf sortProject];
        }];
        [self.containerTostView addSubview:_projectSizeDropDownView];
        self.containerTostView.hidden = NO;
        _projectCategoryDropDownView.hidden = YES;
        _projectBelongDropDownView.hidden = YES;
    }
}

- (UIView *)containerTostView{
    if (!_containerTostView) {
        _containerTostView = [[UIView alloc] initWithFrame:CGRectMake(0, _headView.bounds.size.height, SCREEN_WIDTH, _baseTableView.bounds.size.height - _headView.bounds.size.height)];
        _containerTostView.backgroundColor = [UIColor colorWithHexString:@"#000000" alpha:0.2];
        [_baseTableView addSubview:_containerTostView];
    }
    return _containerTostView;
}


@end
